package com.hnchances.controller;

import com.hnchances.pojo.entity.Result;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    private static final String SMS_KEY = "SMS_CAPTCHA";

    private final RedisTemplate redisTemplate;

    public CaptchaController(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    @GetMapping("/sms")
    public Result<String> generateSmsCaptcha(){

        String code = RandomStringUtils.randomNumeric(6);
        redisTemplate.opsForValue().set(SMS_KEY,code,3, TimeUnit.MINUTES);
        System.out.println("短信验证码："+code);

        return Result.ok(code);
    }

}
